package com.leilei.controller;

import com.leilei.entity.Commodity;
import com.leilei.entity.CommodityColor;
import com.leilei.entity.CommodityVersion;
import com.leilei.entity.User;
import com.leilei.service.ICommodityService;
import com.leilei.service.impl.CommodityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/CommodityServlet")
public class CommodityServlet extends BaseServlet {

    private ICommodityService commodityService = new CommodityServiceImpl();

    public CommodityServlet() {

    }

    /**
     * 查找所有的商品
     */
    protected void getCommodityInIndex(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String stype = req.getParameter("type");

        int type = stype == null ? 0 : Integer.parseInt(stype);

        List<Commodity> commodityList = commodityService.getCommodityInIndex(type);

        req.setAttribute("commodityList", commodityList);

        if (type == 1) {
            req.getRequestDispatcher("jsp/phoneIndex.jsp").forward(req, resp);
        } else {
            req.getRequestDispatcher("jsp/homeAppliance.jsp").forward(req, resp);
        }

    }

    /**
     * 分页查询商品
     */
    protected void getCommodityByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String stype = req.getParameter("type");
        String spage = req.getParameter("page");
        String spageSize = req.getParameter("pageSize");

        int type = stype == null ? 0 : Integer.parseInt(stype);
        int page = spage == null ? 1 : Integer.parseInt(spage);
        int pageSize = spageSize == null ? 5 : Integer.parseInt(spageSize);

        List<Commodity> commodityList = commodityService.getCommodityBypage(type, page, pageSize);

        req.setAttribute("commodityList", commodityList);

        req.getRequestDispatcher("jsp/top.jsp").forward(req, resp);

    }

    /**
     * 根据id查找商品
     */
    protected void getCommodityById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {

            req.getRequestDispatcher("jsp/login.jsp").forward(req, resp);

        } else {

            String spid = req.getParameter("pid");
            int pid = spid == null ? 0 : Integer.parseInt(spid);

            Commodity commodity = commodityService.getCommodityByPid(pid);

            req.setAttribute("commodity", commodity);

            req.getRequestDispatcher("jsp/phoneByPid.jsp").forward(req, resp);
        }

    }

    /**
     * 根据id查找商品
     */
    protected void getUpdateCommodity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String spid = req.getParameter("pid");

        int pid = spid == null ? 0 : Integer.parseInt(spid);

        Commodity commodity = commodityService.getCommodityByPid(pid);
        req.setAttribute("commodity", commodity);

        req.getRequestDispatcher("jsp/updateCommodity.jsp").forward(req, resp);
    }

    /**
     * 更新商品信息
     */
    protected void updateCommodity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String spid = req.getParameter("pid");
        String commodityName = req.getParameter("commodityName");
        String describetest = req.getParameter("describetest");

        int pid = spid == null ? 0 : Integer.parseInt(spid);

        Commodity commodity = new Commodity();
        commodity.setPid(pid);
        commodity.setCommodityName(commodityName);
        commodity.setDescribetest(describetest);

        boolean result = commodityService.updateCommodity(commodity);
        if (result) {
            req.getRequestDispatcher("/CommodityManagerServlet?method=getCommodityByPage&type=1").forward(req, resp);
        } else {
            req.getRequestDispatcher("/CommodityServlet?method=getUpdateCommodity&pid=" + pid).forward(req, resp);
        }

    }

    /**
     * 新增商品信息
     */
    protected void insertCommodity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String stype = req.getParameter("type");
        int type = stype == null ? 0 : Integer.parseInt(stype);

        String commodityName = req.getParameter("commodityName");
        String describetest = req.getParameter("describetest");
        String versionname = req.getParameter("versionname");

        String soprice = req.getParameter("oprice");
        double oprice = soprice == null ? 0.0 : Double.parseDouble(soprice);
        String scprice = req.getParameter("cprice");
        double cprice = scprice == null ? 0.0 : Double.parseDouble(scprice);

        String snum = req.getParameter("num");
        int num = snum == null ? 0 : Integer.parseInt(snum);

        String colorname = req.getParameter("colorname");

        CommodityColor commodityColor = new CommodityColor();
        commodityColor.setColorname(colorname);
        commodityColor.setPdefault(1);
        List<CommodityColor> commodityColorList = new ArrayList<>();
        commodityColorList.add(commodityColor);

        CommodityVersion commodityVersion = new CommodityVersion();
        commodityVersion.setVersionname(versionname);
        commodityVersion.setOprice(oprice);
        commodityVersion.setCprice(cprice);
        commodityVersion.setNum(num);
        commodityVersion.setVdefault(1);
        List<CommodityVersion> commodityVersionList = new ArrayList<>();
        commodityVersionList.add(commodityVersion);

        Commodity commodity = new Commodity();
        commodity.setCtid(type);
        commodity.setCommodityName(commodityName);
        commodity.setDescribetest(describetest);
        commodity.setCommodityColors(commodityColorList);
        commodity.setCommodityVersion(commodityVersionList);

        boolean result = commodityService.insertCommodity(commodity);
        if (result){

            int count = commodityService.getCountByType(type);
            int maxPage = count % 5 == 0 ? count / 5 : count / 5 + 1;

            req.getRequestDispatcher("/CommodityManagerServlet?method=getCommodityByPage&type="+ type +"&currentPage=" + maxPage).forward(req, resp);
        }else{
            req.getRequestDispatcher("jsp/insertCommodity.jsp").forward(req, resp);
        }

    }

    /**
     * 查找所有的商品
     */
    protected void lunboCommodity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        List<Commodity> commodityList = commodityService.lunboCommodity();

        req.setAttribute("commodityList", commodityList);

        req.getRequestDispatcher("jsp/lunbo.jsp").forward(req, resp);

    }

}
